package com.demo.apparel.service.uniapp.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.apparel.common.IdGenerator;
import com.demo.apparel.entity.UserAddress;
import com.demo.apparel.mapper.uniapp.UserAddressMapper;
import com.demo.apparel.service.uniapp.UserAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress>
        implements UserAddressService {

//    @Override
//    public List<UserAddress> listByUserId(String userId) {
//        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
//        wrapper.eq("user_id", userId).orderByDesc("is_default", "created_at");
//        return baseMapper.selectList(wrapper);
//    }
//
//    @Override
//    public void unsetDefault(String userId) {
//        baseMapper.unsetDefault(userId);
//    }
//
//
//    @Override
//    @Transactional
//    public void saveOrUpdateDefault(UserAddress address) {
//        if (address.getIsDefault() != null && address.getIsDefault() == 1) {
//            unsetDefault(address.getUserId());
//        }
//        saveOrUpdate(address);
//    }

    @Override
    public void create(UserAddress address) {
        if (address.getIsDefault() != null && address.getIsDefault() == 1) {
            baseMapper.unsetDefault(address.getUserId());
        }
        if (address.getId() == null || address.getId().isEmpty()) {
            address.setId(IdGenerator.createAddressId());  // 使用雪花算法生成
        }
        address.setCreatedTime(LocalDateTime.now());
        address.setUpdatedTime(LocalDateTime.now());
        this.save(address);
    }

    @Override
    public void update(UserAddress address) {
        if (address.getIsDefault() != null && address.getIsDefault() == 1) {
            baseMapper.unsetDefault(address.getUserId());
        }

        this.updateById(address);
    }

    @Override
    public void delete(String id, String userId) {
        UserAddress exist = this.getById(id);
        if (exist != null && userId.equals(exist.getUserId())) {
            this.removeById(id);
        }
    }

    @Override
    public UserAddress getById(String id, String userId) {
        UserAddress address = super.getById(id);
        if (address != null && userId.equals(address.getUserId())) {
            return address;
        }
        return null;
    }

    @Override
    public List<UserAddress> listByUserId(String userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public void saveOrUpdateDefault(UserAddress address) {
        if (address.getIsDefault() != null && address.getIsDefault() == 1) {
            baseMapper.unsetDefault(address.getUserId());
        }
        if (ObjectUtils.isEmpty(address.getId())) {
            this.save(address);
        } else {
            this.updateById(address);
        }
    }
}
